package server

import (
	"github.com/akirakazu/go-simplejson"
)

// Auth using id and password
func (s *Server) auth(id *json.Json, pwd string) bool {
	// Special Connection: proxy
	authed, shouldContinue := s.checkProxy(id, pwd)
	if !shouldContinue {
		return authed
	}

	// TODO - over configuration
	return s.cuAuth(id, pwd)
}

func (s *Server) checkProxy(id *json.Json, pwd string) (bool, bool) {
	if !id.Has("client_type") {
		return false, true
	}

	ct, err := id.Get("client_type").String()
	if err != nil {
		return false, true
	}

	if ct != "__proxy" {
		return false, true
	}

	if pwd == PROXY_PWD {
		return true, false
	} else {
		return false, false
	}
}
